package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookService.insertAddress(addressBook);
        return Result.success();
    }

    /**
     * 获取所有地址
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        Long userId = BaseContext.getCurrentId();
        AddressBook address = AddressBook.builder().userId(userId).build();
        List<AddressBook> list = addressBookService.list(address);
        return Result.success(list);
    }

    /**
     * 根据id查询对应地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBook(@PathVariable Long id){
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder()
                .id(id)
                .userId(userId)
                .build();
        AddressBook address = addressBookService.getAddressBookById(addressBook);
        return Result.success(address);
    }

    private final Integer DEFAULT= 0;
    private final Integer isDEFAULT= 1;

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook){
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookService.updateAddressById(addressBook);
        return Result.success();
    }

    /**
     * 更改默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result setAddressBookDefault(@RequestBody AddressBook addressBook){
        log.info("要修改的Id为:{}",addressBook);
        Long id = addressBook.getId();
        Long userId = BaseContext.getCurrentId();
        AddressBook address = AddressBook.builder()
                .id(id)
                .userId(userId)
                .build();
        addressBookService.isDefault(address);
        return Result.success();
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public Result< List<AddressBook>> getDefaultAddressBook(){
        Long userId = BaseContext.getCurrentId();
        AddressBook address = AddressBook.builder().userId(userId).isDefault(isDEFAULT).build();
        List<AddressBook> defaultAddress = addressBookService.list(address);
        if (defaultAddress.size()>0 && defaultAddress.size() == isDEFAULT){
            return Result.success(defaultAddress);
        }
        return Result.error("未查询到默认地址");
    }

    /**
     * 根基id删除地址
     * @param addressBook
     * @return
     */
    @DeleteMapping
    public Result deleteAddressBook(@RequestBody AddressBook addressBook){
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookService.deleteAddressById(addressBook);
        return Result.success();
    }

}
